package com.mes.task.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.check.domain.CheckTask;
import com.mes.common.dao.BasicDao;
import com.mes.task.dao.iface.IAssignmentDeviceDao;
import com.mes.task.domain.AssignmentDevice;
import com.mes.task.domain.OperationTask;
import com.mes.task.domain.OperationTaskLink;

@Repository
public class AssignmentDeviceDao extends BasicDao<AssignmentDevice> implements IAssignmentDeviceDao {

	private final static Log log = LogFactory.getLog(AssignmentDeviceDao.class);

	public AssignmentDeviceDao() {
		super();
	}

	/**
	 * 通过工序任务唯一号获取派工信息列表
	 * 
	 * @param operationTaskUid
	 * @return
	 */
	public List<AssignmentDevice> getAssignmentDevices(String operationTaskUid) {
		List list = null;
		try {
			Session session = this.getSessionFactory().getCurrentSession();
			Criteria crit = session.createCriteria(AssignmentDevice.class);
			crit.add(Restrictions.eq("operationTaskUid", operationTaskUid));
			list = crit.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 获取某一部门一段时间的未完派工记录,按照派工时间进行排序
	 * 
	 * @param deptUid
	 * @param page
	 * @return
	 */
	@Override
	public List<AssignmentDevice> getDeptAssignments(String deptUid, Page<AssignmentDevice> page) {
		List list = null;
		try {
			DetachedCriteria crit = DetachedCriteria.forClass(AssignmentDevice.class);
			crit.add(Restrictions.eq("operationTask.masterShopUid", deptUid));
			crit.add(Restrictions.lt("operationTask.taskState", OperationTask.TASKSTATE_COMPLETED));
			Integer[] assnStates = { 0, 1 };
			crit.add(Restrictions.in("assnState", assnStates));
			crit.addOrder(Property.forName("operationTask.taskState").desc())
					.addOrder(Property.forName("assnStart").asc());
			if (page != null) {
				this.pagingQuery(page, crit);
			} else {
				list = this.query(crit, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 获取某一部门所有未完成工序的 某一状态下的派工记录,按照状态过滤
	 * 
	 * @param deptUid
	 * @param assnState
	 * @param page
	 * @return
	 */
	@Override
	public List<AssignmentDevice> getAssignmentDevices(String deptUid, int assnState, Page<AssignmentDevice> page) {
		List list = null;
		try {
			DetachedCriteria crit = DetachedCriteria.forClass(AssignmentDevice.class);
			crit.add(Restrictions.eq("assnState", assnState));
			crit.add(Restrictions.lt("operationTask.taskState", OperationTask.TASKSTATE_COMPLETED));
			crit.add(Restrictions.like("operationTask.masterShopUid", deptUid, MatchMode.END));
			if (page != null) {
				this.pagingQuery(page, crit);
			} else {
				list = this.query(crit, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 获取某一部门的已下达的派工记录
	 * 
	 * @param deptUid
	 * @param page
	 * @return
	 */
	@Override
	public List<AssignmentDevice> getStartedAssignmentDevices(String deptUid, Page<AssignmentDevice> page) {
		return getAssignmentDevices(deptUid, AssignmentDevice.ASSNSTATE_STARTED, page);
	}

	/**
	 * 获取某一部门还未发生作用的派工单
	 * 
	 * @param deptUid
	 * @param page
	 * @return
	 */
	@Override
	public List<AssignmentDevice> getUnWorkAssignmentDevices(String deptUid, Page<AssignmentDevice> page) {
		List list = null;
		try {
			DetachedCriteria crit = DetachedCriteria.forClass(AssignmentDevice.class);
			Integer[] assnStates = { 0, 1 };
			crit.add(Restrictions.in("assnState", assnStates));
			crit.add(Restrictions.eq("operationTask.taskState", OperationTask.TASKSTATE_DISPATCHED));
			crit.add(Restrictions.like("device.deptUid", deptUid, MatchMode.END));
			if (page != null) {
				this.pagingQuery(page, crit);
			} else {
				list = this.query(crit, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<AssignmentDevice> getDeviceUnFinishedAssignments(String deviceUid, Page<AssignmentDevice> page) {
		List list = null;
		try {
			DetachedCriteria crit = DetachedCriteria.forClass(AssignmentDevice.class);
			crit.createAlias("operationTask", "t");
			crit.add(Restrictions.eq("deviceUid", deviceUid));
			crit.add(Restrictions.lt("t.taskState", OperationTask.TASKSTATE_COMPLETED));
			Integer[] assnStates = { 0, 1 };
			crit.add(Restrictions.in("assnState", assnStates));
			crit.add(Restrictions.gt("t.planQty", 0)); // 加上大于0为了防止那种派工后又被取消的任务
			// 保持已开始的任务在最前面
			crit.addOrder(Order.desc("t.taskState")).addOrder(Order.asc("t.planStart")).addOrder(Order.asc("t.batchNum"));
			if (page != null) {
				this.pagingQuery(page, crit);
			} else {
				list = this.query(crit, null);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

}
